<template>
  <div
    id="lineChart"
    ref="lineChart"
    :style="{ width: props.width, height: props.height }"
  ></div>
</template>
<script lang="ts" setup>
import { prop } from "dom7";
import * as echarts from "echarts";
import { EChartsOption, EChartsType } from "echarts";
import { onMounted, onUnmounted, ref } from "vue";

const lineChart = ref(null);
const props = defineProps({
  width: {
    type: String,
    default: "100%",
  },
  height: {
    type: String,
    default: "100%",
  },
  xData: {
    type: Array<string>,
    default: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  },
  seriesData: {
    type: Array<number>,
    default: [150, 230, 224, 218, 135, 147, 260],
  },
  config: {
    default: {},
  },
});
// 线性表的配置项
const option = ref<EChartsOption>({
  ...props.config,
  xAxis: {
    type: "category",
    data: props.xData,
  },
  yAxis: {
    type: "value",
  },
  series: [
    {
      data: props.seriesData,
      type: "line",
    },
  ],
  grid: {
    top: 10,
    left: "2%",
    right: "2%",
    bottom: "2%",
    containLabel: true,
  },
});

const initChart = () => {
  const myChart: EChartsType = echarts.init(lineChart.value!);
  myChart.setOption(option.value);
  window.addEventListener("resize", () => myChart.resize());
};

onMounted(() => {
  initChart();
});
onUnmounted(() => {
  window.addEventListener("resize", null);
});
</script>
<style lang="less" rel="stylesheet/less" scoped></style>
